/// @tags:
#include <cctype>
#include <cstdio>
#include <cstring>

typedef long long ll;

template <typename T>
inline T &read(T &x) {
  x = 0;
  bool f = false;
  short ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = true;
    ch = getchar();
  }
  while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ '0'), ch = getchar();
  if (f) x = -x;
  return x;
}

const int N = 5e4 + 5, bit = 11;
int n, a[N];
int e_cnt, heads[N];
int f[N][bit][2], g[N][bit][2], cnt[N][bit][2], gcnt[N][bit][2];
ll ans;

struct Edge {
  int v, nxt;
} e[N << 1];

inline void add(int u, int v) {
  e[++e_cnt].v = v, e[e_cnt].nxt = heads[u], heads[u] = e_cnt;
}

void pre(int u) {
  for (int j = 0; j < bit; ++j) ++cnt[u][j][(a[u] >> j) & 1];
  for (int i = heads[u], v; i; i = e[i].nxt) {
    pre(v = e[i].v);
    for (int j = 0; j < bit; ++j) {
      cnt[u][j][0] += cnt[v][j][0];
      cnt[u][j][1] += cnt[v][j][1];
      f[u][j][0] += f[v][j][0] + cnt[v][j][0];
      f[u][j][1] += f[v][j][1] + cnt[v][j][1];
    }
  }
}

void solve(int u) {
  for (int j = 0; j < bit; ++j) {
    ans += (1ll << j) * g[u][j][!((a[u] >> j) & 1)];
  }
  for (int i = heads[u], v; i; i = e[i].nxt) {
    v = e[i].v;
    for (int j = 0; j < bit; ++j) {
      int tmp = g[u][j][0] - f[v][j][0] - cnt[v][j][0],
          tmpcnt = gcnt[u][j][0] - cnt[v][j][0];
      g[v][j][0] += tmp + tmpcnt;
      gcnt[v][j][0] += tmpcnt;

      tmp = g[u][j][1] - f[v][j][1] - cnt[v][j][1],
      tmpcnt = gcnt[u][j][1] - cnt[v][j][1];
      g[v][j][1] += tmp + tmpcnt;
      gcnt[v][j][1] += tmpcnt;
    }
    solve(v);
  }
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen64("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen64("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("hide.in", "r", stdin);
  freopen("hide.out", "w", stdout);
#endif
#endif

  read(n);
  for (int i = 2, f; i <= n; ++i) { add(read(f), i); }
  for (int i = 1; i <= n; ++i) read(a[i]);
  pre(1);
  memcpy(g, f, sizeof(g));
  memcpy(gcnt, cnt, sizeof(gcnt));
  solve(1);
  printf("%lld", ans >> 1);
  return 0;
}